﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using Silmoon.Threading;
using System.Threading;
using System.Net.Sockets;

namespace Cooky.Face
{
    public partial class CheckDomainForm : Silmoon.Windows.Forms.GenieForm
    {
        public CheckDomainForm()
        {
            CheckForIllegalCrossThreadCalls = false;
            InitializeComponent();
        }

        private void ctlStartCheckButton_Click(object sender, EventArgs e)
        {
            label1.ForeColor = Color.Orange;
            label2.ForeColor = Color.Orange;

            if (textBox1.Text == "")
            {
                label1.Text = "木有写域名。";
                label2.Text = "咱先把域名填上好吧？";
                return;
            }
            ctlStartCheckButton.Enabled = false;
            textBox1.Enabled = false;
            progressBar1.Visible = true;
            progressBar1.Value = 1;
            Threads.ExecAsync(_thread, null);
        }

        void _thread()
        {

            if (!startCheckProcess())
            {
                label1.ForeColor = Color.Red;
                label2.ForeColor = Color.Red;
            }
            else
            {
                label1.ForeColor = Color.YellowGreen;
                label2.ForeColor = Color.YellowGreen;
            }
            ctlStartCheckButton.Enabled = true;
            textBox1.Enabled = true;
            progressBar1.Visible = false;
            progressBar1.Value = 0;
        }
        bool startCheckProcess()
        {
            label1.Text = "目前没有发现问题...准备解析域名";
            label2.Text = "解析域名中...";
            IPAddress[] ips = null;
            try
            {
                ips = Dns.GetHostAddresses(textBox1.Text);
            }
            catch
            {
                label1.Text = "域名无法解析...";
                label2.Text = "域名无法解析为IP地址...";
                return false;
            }

            label2.Text = "域名解析成功...得到" + ips.Length + "个IP地址，第一个IP地址为(" + ips[0].ToString() + ")";
            Thread.Sleep(1000);
            label1.Text = "目前没有发现问题...准备连接域名指向的网站服务器";
            label2.Text = "正在准备解析域名...";
            Socket socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            socket.ReceiveTimeout = 5000;
            label1.Text = "目前没有发现问题...";
            label2.Text = "连接" + ips[0].ToString() + "...";
            try
            {
                socket.Connect((EndPoint)new IPEndPoint(ips[0], 80));
            }
            catch
            {
                socket.Close();
                label1.Text = "域名解析没有问题，但是指向的网站服务器无法连接...";
                label2.Text = "连接" + ips[0].ToString() + "失败...";
                return false;
            }
            label1.Text = "目前没有发现问题，服务器连接成功...";
            label2.Text = "准备发起网站访问请求...";

            byte[] sendData = Encoding.Default.GetBytes("GET / HTTP/1.1\r\nHOST: " + textBox1.Text + "\r\n\r\n");
            socket.Send(sendData);

            try
            {
                byte[] recvData = new byte[512];
                int len = socket.Receive(recvData);
                string recvString = Encoding.Default.GetString(recvData, 0, len);
                string[] recvsarr = recvString.Split(new string[] { " " }, StringSplitOptions.None);
                if (recvsarr.Length > 1)
                {
                    if (recvsarr[1] == "200")
                    {

                    }
                }
                else
                {
                    label1.Text = "域名正常，网站服务器异常...";
                    label2.Text = "网站服务器返回有问题...";
                }
            }
            catch
            {
                socket.Close();
                label1.Text = "域名正常，网站服务器响应超时...";
                label2.Text = "网站服务器响应超时...";
                return false;
            }
            label1.Text = "域名正常，域名指向的网站服务器正常...";
            label2.Text = "检查已经成功完成...";

            return true;
        }

        private void textBox1_Enter(object sender, EventArgs e)
        {
            textBox1.BackColor = Color.FromArgb(220, 255, 220);
        }

        private void textBox1_Leave(object sender, EventArgs e)
        {
            textBox1.BackColor = Color.FromArgb(210, 255, 210);
        }

        private void ctlStartCheckButton_EnabledChanged(object sender, EventArgs e)
        {
            if (ctlStartCheckButton.Enabled)
            {
                ctlStartCheckButton.BackColor = Color.FromArgb(15, 177, 255);
            }
            else
            {
                ctlStartCheckButton.BackColor = Color.FromArgb(220,220,220);
            }
        }

        private void ctlCloseButton_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
            Close();
        }
    }
}
